package com.hy.usercenter.utils.once;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

import com.hy.usercenter.model.domain.SysUser;
import com.hy.usercenter.service.SysUserService;
import lombok.extern.java.Log;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * @Description 编写一次性任务
 *
 * @Author minsf
 * @Date 2023/2/21
 */
@Component
@Log
public class UserInsert {

       private SysUserService userService;

    /**
     * 批量插入用户
     * fixedDelay = 3000 延迟3秒执行 ，不指定cron会每隔3s执行一次
     *
     * 1000  一次1,90s
     * 100000 一次1000,20s
     *
     *
     */
    //@Scheduled(initialDelay = 3000, fixedDelay = Long.MAX_VALUE)
    public void batchUser() {
        // spring提供的计时器
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        final int INSERT_NUM = 100000;

        // 容器--批量存入用户数据
        List<SysUser> list = new ArrayList<>();
        for (int i = 0; i < INSERT_NUM; i++) {
            SysUser user = new SysUser();
            user.setId(0L);
            user.setUserName("");
            user.setUserAccount("");
            user.setUserPassword("");
            user.setAvatarUrl("");
            user.setGender(0);
            user.setPhone("");
            user.setEmail("");
            user.setUserRole(0);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setIsDeleted(0);
            user.setUserStatus(0);
            user.setTags("");
            user.setProfile("");
            list.add(user);
        }
        userService.saveBatch(list, 100);
        stopWatch.getTotalTimeMillis();
        log.info("+++++++++++++++++++++");
        log.info("插入耗时 "+stopWatch.getTotalTimeMillis()+"ms");
        log.info("+++++++++++++++++++++");
    }

    private ExecutorService executor = new ThreadPoolExecutor(60, 1000,
            10000, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1005));

    public void doConcurrencyInsert() {
        // spring提供的计时器
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        final int INSERT_NUM = 100000;
        // 分十组
        int j = 10;
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<SysUser> userList = new ArrayList<>();
            while (true) {
                j++;
                SysUser user = new SysUser();
                user.setId(0L);
                user.setUserName("");
                user.setUserAccount("");
                user.setUserPassword("");
                user.setAvatarUrl("");
                user.setGender(0);
                user.setPhone("");
                user.setEmail("");
                user.setUserRole(0);
                user.setCreateTime(new Date());
                user.setUpdateTime(new Date());
                user.setIsDeleted(0);
                user.setUserStatus(0);
                user.setTags("");
                user.setProfile("");
                userList.add(user);
                if (i % 10000 == 0) {
                    // 每次一万条
                    break;
                }
            }
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                userService.saveBatch(userList, 10000);
            }, executor); // 建议用自定义线程池
            // 把这个future加入list
            futureList.add(future);
        }
        // 执行这是个异步任务
        // 使用 join，将所有异步任务阻塞，直到所有异步任务都执行完成
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();
        stopWatch.getTotalTimeMillis();
        log.info("+++++++++++++++++++++");
        log.info("插入耗时 "+stopWatch.getTotalTimeMillis()+"ms");
        log.info("+++++++++++++++++++++");
    }



}
